package com.ch02.service;

import com.ch02.mapper.DormitoryMapper;
import com.ch02.pojo.Dormitory;
import com.ch02.utils.MyBatisSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

/**
 * @ClassName DormitoryService
 * @Description TODO
 * @Author Kx
 * @Date 2025/4/23 15:37
 * @Version 1.0
 */
public class DormitoryService {
    public void insertDormitory(Dormitory dormitory) {
        // 获取sqlSession对象
        SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();
        try {
            // 获取mapper代理对象
            DormitoryMapper mapper = sqlSession.getMapper(DormitoryMapper.class);

            // 调用方法
            mapper.insertDormitory(dormitory);

            // 提交事务
            sqlSession.commit();
        } catch (RuntimeException e) {
            // 事物回滚
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            // 关闭sqlSession对象
            sqlSession.close();
        }
    }
}
